function gadget:GetInfo()
	return {
		name = "experience",
		desc = "dole out experience for this wonderful game",
		author = "Sean Heron",
		date = "2009-12-31",
		license = "GNU GPL v2, or later",
		layer = 25,			-- needs to be lower than other level related stuff - setting global vars before they're called by others, yes ? YES! - broken otherwise...
		enabled = true
	}
end

local debugon = false

-- the engines list is loaded in gadget:Initialise (from a shared config file)

local expRange = 1000

if (gadgetHandler:IsSyncedCode()) then

--SYNCED

local levelUp = {}
local speclevelUp = {}  -- for use by the individual Engine scripts - as opposed to levelUp which is checked by the levelup gadget

local engineXP={}
local engineLvlup ={
			[1] = 2,
			[2] = 6,
			[3] = 12,
			[4] = 24,
			[5] = 38,		-- just some numbers for now - could be used for cumulative, or that many each level
			[6] = 54,
			[7] = 78,
			[8] = 128,
			[9] = 256,
			[10] = 12345678, -- catchall - am lazy
			}
--Spring.SendToUnsynced("XP for Level up", engineLvlup)

local engineLevel = {}		-- need to assign each player an enginelevel (and EngineXP)
							-- as units are respawned unitID probably not convenient
							-- Playername? but may not be unique, no? Ah - Player ID!!
function gadget:UnitCreated(u, ud, team)  -- team is as good as playerID ? not sharing are we (and if we were, we'd want the Engine lvl to be shared as well, huh)

end

function gadget:UnitDestroyed(u, ud, team, attacker) 

	--get all units around the destroyed one
	local x,y,z = Spring.GetUnitPosition(u)
	local unitsAround = Spring.GetUnitsInCylinder(x,z, expRange)
	
	--now check out for enemy heros around the destroyed unit
	local enemyHerosAround = {}
	for index,unitIDLoop in pairs(unitsAround) do
		if (team~=nil)and(Spring.GetUnitTeam(unitIDLoop)~=nil) then
			if (not Spring.AreTeamsAllied(Spring.GetUnitTeam(unitIDLoop),team)) then
				if (engines[Spring.GetUnitDefID(unitIDLoop)]~=nil) then
		 		enemyHerosAround[unitIDLoop] = engines[Spring.GetUnitDefID(unitIDLoop)]
		 		end
			end
		end
	end
	
	--distribute the experience
	local n = 0 --first count the elements of enemyHerosAround, I'm sure there's a better way than this but enemyHerosAround.n doesn't work. Do you know it?
	for unitIDLoop,factory in pairs(enemyHerosAround) do
	 	n = n + 1
	end
	for unitIDLoop,factory in pairs(enemyHerosAround) do --now the experience can be distributed
		engineXP[Spring.GetUnitTeam(unitIDLoop)] = engineXP[Spring.GetUnitTeam(unitIDLoop)] + (1/n)
		_G.Levelstuff = {uengineLvlup=engineLvlup,uengineXP=engineXP} --this is from sean, no idea for what...
		Spring.Echo("Hero gains EP")
	end
	
	--at last check if there is a level up
	for unitIDLoop,factory in pairs(enemyHerosAround) do
		if engineXP[Spring.GetUnitTeam(unitIDLoop)] >= engineLvlup[engineLevel[Spring.GetUnitTeam(unitIDLoop)]] then
			engineLevel[Spring.GetUnitTeam(unitIDLoop)] = engineLevel[Spring.GetUnitTeam(unitIDLoop)] + 1 
			levelUp[Spring.GetUnitTeam(unitIDLoop)] = unitIDLoop-- being set to a number (the UnitID of the Engine leveling up) amounts to the value being set to "true"
			speclevelUp[Spring.GetUnitTeam(unitIDLoop)] = unitIDLoop
			_G.uengineLevel=engineLevel
			Spring.Echo("Hero Lvl Up")
		end
	end
	
	
	--############################################### the old code of experience management - uncomment if needed ##################################################
	-- doesn't account for the case of self destruction (which we will probably disallow anyway...)
	--Spring.Echo("Unit destroyed!")
	--[[if attacker then -- check in case the attacker has been destroyed before our unit was
		attackerDef = Spring.GetUnitDefID(attacker)		-- the way its made now is a bit messed up if the attacker is destroyed before the UnitDestroyed call is made
		attackerTeam = Spring.GetUnitTeam(attacker)
		if engines[attackerDef] then -- also need to check it was from enemy allyteam! no XP for destroying own units!!
			engineXP[attackerTeam] = engineXP[attackerTeam] + 1
			if debugon then
				Spring.Echo(attacker)
				Spring.Echo("now has destroyed unit")
				Spring.Echo(engineXP[attackerTeam])
			end
			_G.Levelstuff = {uengineLvlup=engineLvlup,uengineXP=engineXP} -- needs to be split up some (ie should be called for levelup as well) - but will work from here
			if engineXP[attackerTeam] >= engineLvlup[engineLevel[attackerTeam]] 
			
			--[[then -- check if this is correct notation for greaterequals
				engineLevel[attackerTeam] = engineLevel[attackerTeam] + 1 	--  needs to be global
				levelUp[attackerTeam] = attacker				-- being set to a number (the UnitID of the Engine leveling up) amounts to the value being set to "true"
				speclevelUp[attackerTeam] = attacker
				-- could we not pass on the UnitID here as well ? Then wouldn't need to fuck about at the other end of things...
				_G.uengineLevel=engineLevel
				if debugon then
					Spring.Echo(attacker)
					Spring.Echo("has leveled up! To level")
					Spring.Echo(engineLevel[attackerTeam])
				end
				-- engineXP[team] = 0 --- (if wanted so)
				-- might want to pass on special levelup call - but could also check via routine in Engine scripts
			end
		end
	end]]
	--##################################################################################################################################################################
end

function gadget:Initialize()  -- something along these lines probably wanted/ needed for my global variables

	-- GG.engineXP = engineXP -- don't really need
	-- GG.engineLvlup = engineLvlup -- also not at the moment
	GG.engineLevel = engineLevel
	GG.Levelup = levelUp
	GG.specLevelup = speclevelUp

	_, engines, _, _ = include("LuaRules/Configs/Engine_defs.lua")
	
	local teams = Spring.GetTeamList()
	
	for _,teamID in pairs(teams) do
		engineXP[teamID] = 0
		engineLevel[teamID] = 1
		levelUp[teamID] = 0  -- 0 used as equivalent of false
		speclevelUp[teamID] = 0  -- same for our second global pass on levelup value
		Spring.Echo("set team number")
		Spring.Echo(teamID)
	end
	
	
	
	_G.Levelstuff = {uengineLvlup=engineLvlup,uengineXP=engineXP}
	_G.uengineLevel=engineLevel
	
	
end

else
--UNSYNCED

-- local uengineLevel = {}
-- local uengineXP = {}
-- local uengineLvlup = {}

local playerLvl = 1
local playerKills = 4
local nextlvlKills = 8

function gadget:DrawScreen(vsx,vsy)
	if not Spring.IsGUIHidden() and playerLvl then
		local left = vsx*.5
		local right = vsx*.95   -- mumbo jumbo positions just taken from Cuberors - need to change
		local top = vsy-15
		local bottom = top - 20
		gl.Text("Experience / Next level at : "..playerKills.." / "..nextlvlKills, left, top - 10, 16,"oc") -- what's the oc vs or mean ?
		gl.Text("Level: "..playerLvl, right,bottom-5,16,"or")  -- works nicely! now just need to fix the variables...
	end
end


function gadget:Update()
	local teamID = Spring.GetLocalTeamID()
	playerLvl = SYNCED.uengineLevel[teamID]
	playerKills = SYNCED.Levelstuff.uengineXP[teamID]
	nextlvlKills =	SYNCED.Levelstuff.uengineLvlup[playerLvl]

	if debugon then
		Spring.Echo("teamID is")
		Spring.Echo(teamID)		-- so far so good
		Spring.Echo(playerLvl)
		Spring.Echo("why the fuck not working ?")  -- maybe because of the Synced / unsynced firewall ?? indeed it was!
		Spring.Echo(SYNCED.uengineLevel[teamID])
	end
end

end